package com.linyaonan.leetcode.medium._222;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author: Lin
 * @date: 2020/1/19
 */
public class CountCompleteTreeNodes {
    int sum;
    LinkedList<TreeNode> save;

    public int countNodes(TreeNode root) {
        if (root == null) {
            return 0;
        }
        save = new LinkedList<>();
        save.push(root);
        boolean getIt = false;
        while(!save.isEmpty() || getIt) {
            TreeNode pop = save.pop();
            if (pop == null) {
                getIt = true;
            } else {
                sum++;
                if (pop.left != null) {
                    save.push(pop.left);
                }
                if (pop.right != null) {
                    save.push(pop.right);
                }
            }
        }

        return sum;
    }

    public int countNodes2(TreeNode root) {
        if (root == null) {
            return 0;
        }
        return countNodes(root.left) + countNodes(root.right) + 1;
    }
}
